<?php

class SmsLog{

	/**
	 * 添加
	 *
	 * @param unknown_type $name
	 * @param unknown_type $parent
	 * @param unknown_type $ord
	 */
	function add($data){
		
		$data['created']    = full_date();
		$data['ip'] 	    = real_ip();

        $type               = '消息';
        if(isset($data['type']) && !empty($data['type'])){
            $type = $data['type'];
        }
        $data['type'] = $type;

		$m = M('SmsLog');
		//返回自增id
		$id = $m->add($data);
		
		return $id;
	}

    function lists($where,$page,$pageSize,&$allCount){

        $whereSql = ' 1=1 ';

        if(!is_null($where)){

            $where['end_time'] = short_date(strtotime($where['end_time']) + 86400);

            //查询条件
            $whereSql .= " AND (sl.created BETWEEN '{$where['begin_time']}' AND '{$where['end_time']}') ";

            if($where['search']){

                $item = $where['search']['item'];
                $value = $where['search']['value'];

                if($item != '' && $value != ''){
                    $whereSql .= " AND sl.{$item} LIKE '%{$value}%' ";
                }
            }
        }

        //开始行
        $startIndex = ($page-1) * $pageSize;

        //总计数
        $allCount = $this->lists_count($whereSql);

        $m = M('SmsLog');
        return $m->table('sms_log sl')
            ->join('admin a ON a.id = sl.admin_id')
            ->where($whereSql)
            ->order('sl.id DESC')
            ->limit($startIndex. ',' . $pageSize)
            ->field('sl.*,a.real_name')
            ->select();
    }


    function lists_count($whereSql){

        $m = M('SmsLog');
        return $m->table('sms_log sl')
            ->join('admin a ON a.id = sl.admin_id')
            ->where($whereSql)
            ->getField('COUNT(*)');
    }

	
	/**
	 * 修改
	 *
	 * @param unknown_type $id
	 * @param unknown_type $upItems
	 * @return unknown
	 */
	function update($id,$upItems){
		$m = M('SmsLog');
		return $m -> where('id='.$id)->save($upItems);
	}
	
	function get_by_id($id){
		$m = M('SmsLog');
		return $m -> getById($id);
	}
}